package com.hpq.common.thread;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ThreadFactory;

/**
 *  自定义线程工厂(装饰器模式）
 */
@AllArgsConstructor
@Slf4j
public class MyThreadFactory implements ThreadFactory {

    private final ThreadFactory factory;
    @Override
    public Thread newThread(@NotNull Runnable r) {
        Thread thread = factory.newThread(r); // spring自己的创建逻辑
        thread.setUncaughtExceptionHandler(GlobalUncaughtExceptionHandler.getInstance());//额外装饰我们需要的创建逻辑
        return thread;
    }
}
